package com.sanley.piss.web.controller;


import com.sanley.piss.model.exceptionUtil.json.JsonResult;
import com.sanley.piss.model.po.Role;
import com.sanley.piss.web.service.mapper.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 角色控制层
 * Create By  on 2021/3/6.
 * @author 张凯大天才
 */
@Api(value = "角色表controller",tags = "角色表操作接口")
@RestController
@RequestMapping("/role")
public class RolesController {
    /**
     * 角色业务层接口
     */
    @Autowired
    private IRoleService service;



    /**
     * 查所有权限名
     */
    @ApiOperation(value ="查询所有id、权限名、code" )
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public JsonResult<List<Map<Integer, Object>>> selectAllRoleNameAndId() {
        List<Map<Integer, Object>> role = service.selectAllRoleNameAndId();
        return JsonResult.success(role);
    }

    /**
     * 创建新角色
     */
    @ApiOperation(value ="插入一条记录" )
    @RequestMapping(value = "/insert",method = RequestMethod.PUT)
    public JsonResult insert(@Valid Role role) {
        service.insert(role);
        return JsonResult.success();
    }

    @ApiOperation(value = "角色解绑资源")
    @RequestMapping(value = "/{roleId}/removeMenu",method = RequestMethod.DELETE)
    public JsonResult<Object> remove(@PathVariable("roleId") Integer roleId, Integer[] resourceId){
        service.updateAuth(roleId,resourceId);
        return JsonResult.success();
    }

    @ApiOperation(value ="根据ID修改一条记录" )
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public JsonResult<Object> update(@Valid Role role){
        service.updateById(role);
        return JsonResult.success();
    }

    @ApiOperation(value ="角色与资源绑定" )
    @RequestMapping(value = "/{roleId}/toRoleAuth",method = RequestMethod.PUT)
    public JsonResult toRoleAuth(@PathVariable("roleId") Integer roleId, Integer[] resourceId) {
        service.toRoleAuth(roleId, resourceId);
        return JsonResult.success();
    }

    @ApiOperation(value ="更换用户角色，更新用户角色关联表" )
    @RequestMapping(value = "/{userId}/updateUserRole",method = RequestMethod.PUT)
    public JsonResult updateUserRole(@PathVariable("userId") Integer userId, Integer[] roleId) {
        service.updateUserRole(userId, roleId);
        return JsonResult.success();
    }

    @ApiOperation(value ="根据ID删除多条记录")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.DELETE)
    public JsonResult<Object> remove(@PathVariable("ids") Integer[] ids){
        service.removeByIds(ids);
        return JsonResult.success();
    }

    @ApiOperation(value ="查询用户所有权限")
    @RequestMapping(value = "selectUserRole/{userId}",method = RequestMethod.GET)
    public JsonResult<List<Map<Integer, Object>>> selectRolesNameCodeIdByUserId(@ApiParam(name = "userId",value = "用户ID") @PathVariable("userId") Integer userId){
        return JsonResult.success(service.selectRolesNameCodeIdByUserId(userId));
    }



}
